package com.gaowl.gulimall.product.service.impl;

import com.gaowl.gulimall.product.vo.Images;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaowl.common.utils.PageUtils;
import com.gaowl.common.utils.Query;

import com.gaowl.gulimall.product.dao.SkuImagesDao;
import com.gaowl.gulimall.product.entity.SkuImagesEntity;
import com.gaowl.gulimall.product.service.SkuImagesService;


@Service("skuImagesService")
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesDao, SkuImagesEntity> implements SkuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuImagesEntity> page = this.page(
                new Query<SkuImagesEntity>().getPage(params),
                new QueryWrapper<SkuImagesEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 批量保存sku的图片信息
     * @param skuId sku的id
     * @param images 待保存的图片信息们
     */
    @Override
    public void batchSaveSkuImages(Long skuId, List<Images> images) {
        List<SkuImagesEntity> skuImagesEntityList = images.stream().map(image -> {
            SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
            BeanUtils.copyProperties(image, skuImagesEntity);
            skuImagesEntity.setSkuId(skuId);
            return skuImagesEntity;
        }).filter(skuImagesEntity -> {
            // 剔除掉图片地址为空的数据
            return !StringUtils.isEmpty(skuImagesEntity.getImgUrl());
        }).collect(Collectors.toList());

        this.saveBatch(skuImagesEntityList);
    }

}